home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / readline / vi_mode.c < prev    next >
C/C++ Source or Header  |  1995-09-19  |  28KB  |  1,335 lines

  1. /* vi_mode.c -- A vi emulation mode for Bash.
  2.    Derived from code written by Jeff Sparkes (jsparkes@bnr.ca).  */
  3.  
  4. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library, a library for
  7.    reading lines of text with interactive input and history editing.
  8.  
  9.    The GNU Readline Library is free software; you can redistribute it
  10.    and/or modify it under the terms of the GNU General Public License
  11.    as published by the Free Software Foundation; either version 1, or
  12.    (at your option) any later version.
  13.  
  14.    The GNU Readline Library is distributed in the hope that it will be
  15.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #define READLINE_LIBRARY
  29.  
  30. /* **************************************************************** */
  31. /*                                    */
  32. /*            VI Emulation Mode                */
  33. /*                                    */
  34. /* **************************************************************** */
  35. #include "rlconf.h"
  36.  
  37. #if defined (VI_MODE)
  38.  
  39. #include <sys/types.h>
  40.  
  41. #if defined (HAVE_STDLIB_H)
  42. #  include <stdlib.h>
  43. #else
  44. #  include "ansi_stdlib.h"
  45. #endif /* HAVE_STDLIB_H */
  46.  
  47. #if defined (HAVE_UNISTD_H)
  48. #  include <unistd.h>
  49. #endif
  50.  
  51. #include <stdio.h>
  52.  
  53. /* Some standard library routines. */
  54. #include "rldefs.h"
  55. #include "readline.h"
  56. #include "history.h"
  57.  
  58. #ifndef digit_p
  59. #define digit_p(c)  ((c) >= '0' && (c) <= '9')
  60. #endif
  61.  
  62. #ifndef digit_value
  63. #define digit_value(c) ((c) - '0')
  64. #endif
  65.  
  66. #ifndef member
  67. #define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0)
  68. #endif
  69.  
  70. #ifndef isident
  71. #define isident(c) ((pure_alphabetic (c) || digit_p (c) || c == '_'))
  72. #endif
  73.  
  74. #ifndef exchange
  75. #define exchange(x, y) do {int temp = x; x = y; y = temp;} while (0)
  76. #endif
  77.  
  78. #ifndef VI_COMMENT_BEGIN_DEFAULT
  79. #define VI_COMMENT_BEGIN_DEFAULT "#"
  80. #endif
  81.  
  82. #if defined (STATIC_MALLOC)
  83. static char *xmalloc (), *xrealloc ();
  84. #else
  85. extern char *xmalloc (), *xrealloc ();
  86. #endif /* STATIC_MALLOC */
  87.  
  88. /* Variables imported from readline.c */
  89. extern int rl_point, rl_end, rl_mark, rl_done;
  90. extern FILE *rl_instream;
  91. extern int rl_line_buffer_len, rl_explicit_arg, rl_numeric_arg;
  92. extern Keymap _rl_keymap;
  93. extern char *rl_prompt;
  94. extern char *rl_line_buffer;
  95. extern int rl_arg_sign;
  96.  
  97. extern void _rl_dispatch ();
  98.  
  99. extern void rl_extend_line_buffer ();
  100. extern int rl_vi_check ();
  101.  
  102. /* Non-zero means enter insertion mode. */
  103. static int _rl_vi_doing_insert = 0;
  104.  
  105. /* String inserted into the line by rl_vi_comment (). */
  106. char *rl_vi_comment_begin = (char *)NULL;
  107.  
  108. /* *** UNCLEAN *** */
  109. /* Command keys which do movement for xxx_to commands. */
  110. static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
  111.  
  112. /* Keymap used for vi replace characters.  Created dynamically since
  113.    rarely used. */
  114. static Keymap vi_replace_map = (Keymap)NULL;
  115.  
  116. /* The number of characters inserted in the last replace operation. */
  117. static int vi_replace_count = 0;
  118.  
  119. /* If non-zero, we have text inserted after a c[motion] command that put
  120.    us implicitly into insert mode.  Some people want this text to be
  121.    attached to the command so that it is `redoable' with `.'. */
  122. static int vi_continued_command = 0;
  123.  
  124. static int _rl_vi_last_command = 'i';    /* default `.' puts you in insert mode */
  125. static int _rl_vi_last_repeat = 1;
  126. static int _rl_vi_last_arg_sign = 1;
  127. static int _rl_vi_last_motion = 0;
  128. static int _rl_vi_last_search_char = 0;
  129. static int _rl_vi_last_replacement = 0;
  130.  
  131. static int vi_redoing = 0;
  132.  
  133. /* Text modification commands.  These are the `redoable' commands. */
  134. static char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
  135.  
  136. static int rl_digit_loop1 ();
  137.  
  138. void
  139. _rl_vi_reset_last ()
  140. {
  141.   _rl_vi_last_command = 'i';
  142.   _rl_vi_last_repeat = 1;
  143.   _rl_vi_last_arg_sign = 1;
  144.   _rl_vi_last_motion = 0;
  145. }
  146.  
  147. void
  148. _rl_vi_set_last (key, repeat, sign)
  149.      int key, repeat, sign;
  150. {
  151.   _rl_vi_last_command = key;
  152.   _rl_vi_last_repeat = repeat;
  153.   _rl_vi_last_arg_sign = sign;
  154. }
  155.  
  156. /* Is the command C a VI mode text modification command? */
  157. int
  158. rl_vi_textmod_command (c)
  159.      int c;
  160. {
  161.   return (member (c, vi_textmod));
  162. }
  163.  
  164. /* Bound to `.'.  Called from command mode, so we know that we have to
  165.    redo a text modification command.  The default for _rl_vi_last_command
  166.    puts you back into insert mode. */
  167. rl_vi_redo (count, c)
  168.      int count, c;
  169. {
  170.   if (!rl_explicit_arg)
  171.     {
  172.       rl_numeric_arg = _rl_vi_last_repeat;
  173.       rl_arg_sign = _rl_vi_last_arg_sign;
  174.     }
  175.  
  176.   vi_redoing = 1;
  177.   _rl_dispatch (_rl_vi_last_command, _rl_keymap);
  178.   vi_redoing = 0;
  179.  
  180.   return (0);
  181. }
  182.     
  183. /* Yank the nth arg from the previous line into this line at point. */
  184. rl_vi_yank_arg (count, key)
  185.      int count, key;
  186. {
  187.   /* Readline thinks that the first word on a line is the 0th, while vi
  188.      thinks the first word on a line is the 1st.  Compensate. */
  189.   if (rl_explicit_arg)
  190.     rl_yank_nth_arg (count - 1, 0);
  191.   else
  192.     rl_yank_nth_arg ('$', 0);
  193.  
  194.   return (0);
  195. }
  196.  
  197. /* With an argument, move back that many history lines, else move to the
  198.    beginning of history. */
  199. rl_vi_fetch_history (count, c)
  200.      int count, c;
  201. {
  202.   int current = where_history ();
  203.  
  204.   /* Giving an argument of n means we want the nth command in the history
  205.      file.  The command number is interpreted the same way that the bash
  206.      `history' command does it -- that is, giving an argument count of 450
  207.      to this command would get the command listed as number 450 in the
  208.      output of `history'. */
  209.   if (rl_explicit_arg)
  210.     {
  211.       int wanted = history_base + current - count;
  212.       if (wanted <= 0)
  213.         rl_beginning_of_history (0, 0);
  214.       else
  215.         rl_get_previous_history (wanted);
  216.     }
  217.   else
  218.     rl_beginning_of_history (count, 0);
  219.   return (0);
  220. }
  221.  
  222. /* Search again for the last thing searched for. */
  223. rl_vi_search_again (count, key)
  224.      int count, key;
  225. {
  226.   switch (key)
  227.     {
  228.     case 'n':
  229.       rl_noninc_reverse_search_again (count, key);
  230.       break;
  231.  
  232.     case 'N':
  233.       rl_noninc_forward_search_again (count, key);
  234.       break;
  235.     }
  236.   return (0);
  237. }
  238.  
  239. /* Do a vi style search. */
  240. rl_vi_search (count, key)
  241.      int count, key;
  242. {
  243.   switch (key)
  244.     {
  245.     case '?':
  246.       rl_noninc_forward_search (count, key);
  247.       break;
  248.  
  249.     case '/':
  250.       rl_noninc_reverse_search (count, key);
  251.       break;
  252.  
  253.     default:
  254.       ding ();
  255.       break;
  256.     }
  257.   return (0);
  258. }
  259.  
  260. /* Completion, from vi's point of view. */
  261. rl_vi_complete (ignore, key)
  262.      int ignore, key;
  263. {
  264.   if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
  265.     {
  266.       if (!whitespace (rl_line_buffer[rl_point + 1]))
  267.     rl_vi_end_word (1, 'E');
  268.       rl_point++;
  269.     }
  270.  
  271.   if (key == '*')
  272.     rl_complete_internal ('*');    /* Expansion and replacement. */
  273.   else if (key == '=')
  274.     rl_complete_internal ('?');    /* List possible completions. */
  275.   else if (key == '\\')
  276.     rl_complete_internal (TAB);    /* Standard Readline completion. */
  277.   else
  278.     rl_complete (0, key);
  279.  
  280.   if (key == '*' || key == '\\')
  281.     {
  282.       _rl_vi_set_last (key, 1, rl_arg_sign);
  283.       rl_vi_insertion_mode (1, key);
  284.     }
  285.   return (0);
  286. }
  287.  
  288. /* Tilde expansion for vi mode. */
  289. rl_vi_tilde_expand (ignore, key)
  290.      int ignore, key;
  291. {
  292.   rl_tilde_expand (0, key);
  293.   _rl_vi_set_last (key, 1, rl_arg_sign);    /* XXX */
  294.   rl_vi_insertion_mode (1, key);
  295.   return (0);
  296. }
  297.  
  298. /* Previous word in vi mode. */
  299. rl_vi_prev_word (count, key)
  300.      int count, key;
  301. {
  302.   if (count < 0)
  303.     return (rl_vi_next_word (-count, key));
  304.  
  305.   if (rl_point == 0)
  306.     {
  307.       ding ();
  308.       return (0);
  309.     }
  310.  
  311.   if (uppercase_p (key))
  312.     rl_vi_bWord (count);
  313.   else
  314.     rl_vi_bword (count);
  315.  
  316.   return (0);
  317. }
  318.  
  319. /* Next word in vi mode. */
  320. rl_vi_next_word (count, key)
  321.      int count, key;
  322. {
  323.   if (count < 0)
  324.     return (rl_vi_prev_word (-count, key));
  325.  
  326.   if (rl_point >= (rl_end - 1))
  327.     {
  328.       ding ();
  329.       return (0);
  330.     }
  331.  
  332.   if (uppercase_p (key))
  333.     rl_vi_fWord (count);
  334.   else
  335.     rl_vi_fword (count);
  336.   return (0);
  337. }
  338.  
  339. /* Move to the end of the ?next? word. */
  340. rl_vi_end_word (count, key)
  341.      int count, key;
  342. {
  343.   if (count < 0)
  344.     {
  345.       ding ();
  346.       return -1;
  347.     }
  348.  
  349.   if (uppercase_p (key))
  350.     rl_vi_eWord (count);
  351.   else
  352.     rl_vi_eword (count);
  353.   return (0);
  354. }
  355.  
  356. /* Move forward a word the way that 'W' does. */
  357. rl_vi_fWord (count)
  358.      int count;
  359. {
  360.   while (count-- && rl_point < (rl_end - 1))
  361.     {
  362.       /* Skip until whitespace. */
  363.       while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  364.     rl_point++;
  365.  
  366.       /* Now skip whitespace. */
  367.       while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  368.     rl_point++;
  369.     }
  370.   return (0);
  371. }
  372.  
  373. rl_vi_bWord (count)
  374.      int count;
  375. {
  376.   while (count-- && rl_point > 0)
  377.     {
  378.       /* If we are at the start of a word, move back to whitespace so
  379.      we will go back to the start of the previous word. */
  380.       if (!whitespace (rl_line_buffer[rl_point]) &&
  381.       whitespace (rl_line_buffer[rl_point - 1]))
  382.     rl_point--;
  383.  
  384.       while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  385.     rl_point--;
  386.  
  387.       if (rl_point > 0)
  388.     {
  389.       while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point]));
  390.       rl_point++;
  391.     }
  392.     }
  393.   return (0);
  394. }
  395.  
  396. rl_vi_eWord (count)
  397.      int count;
  398. {
  399.   while (count-- && rl_point < (rl_end - 1))
  400.     {
  401.       if (!whitespace (rl_line_buffer[rl_point]))
  402.     rl_point++;
  403.  
  404.       /* Move to the next non-whitespace character (to the start of the
  405.      next word). */
  406.       while (++rl_point < rl_end && whitespace (rl_line_buffer[rl_point]));
  407.  
  408.       if (rl_point && rl_point < rl_end)
  409.     {
  410.       /* Skip whitespace. */
  411.       while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  412.         rl_point++;
  413.  
  414.       /* Skip until whitespace. */
  415.       while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point]))
  416.         rl_point++;
  417.  
  418.       /* Move back to the last character of the word. */
  419.       rl_point--;
  420.     }
  421.     }
  422.   return (0);
  423. }
  424.  
  425. rl_vi_fword (count)
  426.      int count;
  427. {
  428.   while (count-- && rl_point < (rl_end - 1))
  429.     {
  430.       /* Move to white space (really non-identifer). */
  431.       if (isident (rl_line_buffer[rl_point]))
  432.     {
  433.       while (isident (rl_line_buffer[rl_point]) && rl_point < rl_end)
  434.         rl_point++;
  435.     }
  436.       else /* if (!whitespace (rl_line_buffer[rl_point])) */
  437.     {
  438.       while (!isident (rl_line_buffer[rl_point]) &&
  439.          !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  440.         rl_point++;
  441.     }
  442.  
  443.       /* Move past whitespace. */
  444.       while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  445.     rl_point++;
  446.     }
  447.   return (0);
  448. }
  449.  
  450. rl_vi_bword (count)
  451.      int count;
  452. {
  453.   while (count-- && rl_point > 0)
  454.     {
  455.       int last_is_ident;
  456.  
  457.       /* If we are at the start of a word, move back to whitespace
  458.      so we will go back to the start of the previous word. */
  459.       if (!whitespace (rl_line_buffer[rl_point]) &&
  460.       whitespace (rl_line_buffer[rl_point - 1]))
  461.     rl_point--;
  462.  
  463.       /* If this character and the previous character are `opposite', move
  464.      back so we don't get messed up by the rl_point++ down there in
  465.      the while loop.  Without this code, words like `l;' screw up the
  466.      function. */
  467.       last_is_ident = isident (rl_line_buffer[rl_point - 1]);
  468.       if ((isident (rl_line_buffer[rl_point]) && !last_is_ident) ||
  469.       (!isident (rl_line_buffer[rl_point]) && last_is_ident))
  470.     rl_point--;
  471.  
  472.       while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  473.     rl_point--;
  474.  
  475.       if (rl_point > 0)
  476.     {
  477.       if (isident (rl_line_buffer[rl_point]))
  478.         while (--rl_point >= 0 && isident (rl_line_buffer[rl_point]));
  479.       else
  480.         while (--rl_point >= 0 && !isident (rl_line_buffer[rl_point]) &&
  481.            !whitespace (rl_line_buffer[rl_point]));
  482.       rl_point++;
  483.     }
  484.     }
  485.   return (0);
  486. }
  487.  
  488. rl_vi_eword (count)
  489.      int count;
  490. {
  491.   while (count-- && rl_point < rl_end - 1)
  492.     {
  493.       if (!whitespace (rl_line_buffer[rl_point]))
  494.     rl_point++;
  495.  
  496.       while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  497.     rl_point++;
  498.  
  499.       if (rl_point < rl_end)
  500.     {
  501.       if (isident (rl_line_buffer[rl_point]))
  502.         while (++rl_point < rl_end && isident (rl_line_buffer[rl_point]));
  503.       else
  504.         while (++rl_point < rl_end && !isident (rl_line_buffer[rl_point])
  505.            && !whitespace (rl_line_buffer[rl_point]));
  506.     }
  507.       rl_point--;
  508.     }
  509.   return (0);
  510. }
  511.  
  512. rl_vi_insert_beg (count, key)
  513.      int count, key;
  514. {
  515.   rl_beg_of_line (1, key);
  516.   rl_vi_insertion_mode (1, key);
  517.   return (0);
  518. }
  519.  
  520. rl_vi_append_mode (count, key)
  521.      int count, key;
  522. {
  523.   if (rl_point < rl_end)
  524.     rl_point++;
  525.   rl_vi_insertion_mode (1, key);
  526.   return (0);
  527. }
  528.  
  529. rl_vi_append_eol (count, key)
  530.      int count, key;
  531. {
  532.   rl_end_of_line (1, key);
  533.   rl_vi_append_mode (1, key);
  534.   return (0);
  535. }
  536.  
  537. /* What to do in the case of C-d. */
  538. rl_vi_eof_maybe (count, c)
  539.      int count, c;
  540. {
  541.   return (rl_newline (1, '\n'));
  542. }
  543.  
  544. /* Insertion mode stuff. */
  545.  
  546. /* Switching from one mode to the other really just involves
  547.    switching keymaps. */
  548. rl_vi_insertion_mode (count, key)
  549.      int count, key;
  550. {
  551.   _rl_keymap = vi_insertion_keymap;
  552.   return (0);
  553. }
  554.  
  555. void
  556. _rl_vi_done_inserting ()
  557. {
  558.   if (_rl_vi_doing_insert)
  559.     {
  560.       rl_end_undo_group ();
  561.       /* Now, the text between rl_undo_list->next->start and
  562.      rl_undo_list->next->end is what was inserted while in insert
  563.      mode. */
  564.       _rl_vi_doing_insert = 0;
  565.       vi_continued_command = 1;
  566.     }
  567.   else
  568.     vi_continued_command = 0;
  569. }
  570.  
  571. rl_vi_movement_mode (count, key)
  572.      int count, key;
  573. {
  574.   if (rl_point > 0)
  575.     rl_backward (1);
  576.  
  577. #if 0
  578.   _rl_vi_reset_last ();
  579. #endif
  580.  
  581.   _rl_keymap = vi_movement_keymap;
  582.   _rl_vi_done_inserting ();
  583.   return (0);
  584. }
  585.  
  586. rl_vi_arg_digit (count, c)
  587.      int count, c;
  588. {
  589.   if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
  590.     return (rl_beg_of_line ());
  591.   else
  592.     return (rl_digit_argument (count, c));
  593. }
  594.  
  595. rl_vi_change_case (count, ignore)
  596.      int count, ignore;
  597. {
  598.   char c = 0;
  599.  
  600.   /* Don't try this on an empty line. */
  601.   if (rl_point >= rl_end)
  602.     return (0);
  603.  
  604.   while (count-- && rl_point < rl_end)
  605.     {
  606.       if (uppercase_p (rl_line_buffer[rl_point]))
  607.     c = to_lower (rl_line_buffer[rl_point]);
  608.       else if (lowercase_p (rl_line_buffer[rl_point]))
  609.     c = to_upper (rl_line_buffer[rl_point]);
  610.       else
  611.     {
  612.       /* Just skip over characters neither upper nor lower case. */
  613.       rl_forward (1);
  614.       continue;
  615.     }
  616.  
  617.       /* Vi is kind of strange here. */
  618.       if (c)
  619.     {
  620.       rl_begin_undo_group ();
  621.       rl_delete (1, c);
  622.       rl_insert (1, c);
  623.       rl_end_undo_group ();
  624.       rl_vi_check ();
  625.         }
  626.       else
  627.     rl_forward (1);
  628.     }
  629.   return (0);
  630. }
  631.  
  632. rl_vi_put (count, key)
  633.      int count, key;
  634. {
  635.   if (!uppercase_p (key) && (rl_point + 1 <= rl_end))
  636.     rl_point++;
  637.  
  638.   rl_yank ();
  639.   rl_backward (1);
  640.   return (0);
  641. }
  642.  
  643. rl_vi_check ()
  644. {
  645.   if (rl_point && rl_point == rl_end)
  646.     rl_point--;
  647.   return (0);
  648. }
  649.  
  650. rl_vi_column (count, key)
  651.      int count, key;
  652. {
  653.   if (count > rl_end)
  654.     rl_end_of_line ();
  655.   else
  656.     rl_point = count - 1;
  657.   return (0);
  658. }
  659.  
  660. int
  661. rl_vi_domove (key, nextkey)
  662.      int key, *nextkey;
  663. {
  664.   int c, save;
  665.   int old_end;
  666.  
  667.   rl_mark = rl_point;
  668.   c = rl_read_key ();
  669.   *nextkey = c;
  670.  
  671.   if (!member (c, vi_motion))
  672.     {
  673.       if (digit_p (c))
  674.     {
  675.       save = rl_numeric_arg;
  676.       rl_numeric_arg = digit_value (c);
  677.       rl_digit_loop1 ();
  678.       rl_numeric_arg *= save;
  679.       c = rl_read_key ();    /* real command */
  680.       *nextkey = c;
  681.     }
  682.       else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
  683.     {
  684.       rl_mark = rl_end;
  685.       rl_beg_of_line ();
  686.       _rl_vi_last_motion = c;
  687.       return (0);
  688.     }
  689.       else
  690.     return (-1);
  691.     }
  692.  
  693.   _rl_vi_last_motion = c;
  694.  
  695.   /* Append a blank character temporarily so that the motion routines
  696.      work right at the end of the line. */
  697.   old_end = rl_end;
  698.   rl_line_buffer[rl_end++] = ' ';
  699.   rl_line_buffer[rl_end] = '\0';
  700.  
  701.   _rl_dispatch (c, _rl_keymap);
  702.  
  703.   /* Remove the blank that we added. */
  704.   rl_end = old_end;
  705.   rl_line_buffer[rl_end] = '\0';
  706.   if (rl_point > rl_end)
  707.     rl_point = rl_end;
  708.  
  709.   /* No change in position means the command failed. */
  710.   if (rl_mark == rl_point)
  711.     return (-1);
  712.  
  713.   /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
  714.      word.  If we are not at the end of the line, and we are on a
  715.      non-whitespace character, move back one (presumably to whitespace). */
  716.   if ((to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
  717.       !whitespace (rl_line_buffer[rl_point]))
  718.     rl_point--;
  719.  
  720.   /* If cw or cW, back up to the end of a word, so the behaviour of ce
  721.      or cE is the actual result.  Brute-force, no subtlety. */
  722.   if (key == 'c' && rl_point >= rl_mark && (to_upper (c) == 'W'))
  723.     {
  724.       /* Don't move farther back than where we started. */
  725.       while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
  726.     rl_point--;
  727.  
  728.       /* Posix.2 says that if cw or cW moves the cursor towards the end of
  729.      the line, the character under the cursor should be deleted. */
  730.       if (rl_point == rl_mark)
  731.         rl_point++;
  732.       else
  733.     {
  734.       /* Move past the end of the word so that the kill doesn't
  735.          remove the last letter of the previous word.  Only do this
  736.          if we are not at the end of the line. */
  737.       if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
  738.         rl_point++;
  739.     }
  740.     }
  741.  
  742.   if (rl_mark < rl_point)
  743.     exchange (rl_point, rl_mark);
  744.  
  745.   return (0);
  746. }
  747.  
  748. /* A simplified loop for vi. Don't dispatch key at end.
  749.    Don't recognize minus sign? */
  750. static int
  751. rl_digit_loop1 ()
  752. {
  753.   int key, c;
  754.  
  755.   while (1)
  756.     {
  757.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
  758.       key = c = rl_read_key ();
  759.  
  760.       if (_rl_keymap[c].type == ISFUNC &&
  761.       _rl_keymap[c].function == rl_universal_argument)
  762.     {
  763.       rl_numeric_arg *= 4;
  764.       continue;
  765.     }
  766.  
  767.       c = UNMETA (c);
  768.       if (digit_p (c))
  769.     {
  770.       if (rl_explicit_arg)
  771.         rl_numeric_arg = (rl_numeric_arg * 10) + digit_value (c);
  772.       else
  773.         rl_numeric_arg = digit_value (c);
  774.       rl_explicit_arg = 1;
  775.     }
  776.       else
  777.     {
  778.       rl_clear_message ();
  779.       rl_stuff_char (key);
  780.       break;
  781.     }
  782.     }
  783.   return (0);
  784. }
  785.  
  786. rl_vi_delete_to (count, key)
  787.      int count, key;
  788. {
  789.   int c;
  790.  
  791.   if (uppercase_p (key))
  792.     rl_stuff_char ('$');
  793.   else if (vi_redoing)
  794.     rl_stuff_char (_rl_vi_last_motion);
  795.  
  796.   if (rl_vi_domove (key, &c))
  797.     {
  798.       ding ();
  799.       return -1;
  800.     }
  801.  
  802.   /* These are the motion commands that do not require adjusting the
  803.      mark. */
  804.   if ((strchr (" l|h^0bB", c) == 0) && (rl_mark < rl_end))
  805.     rl_mark++;
  806.  
  807.   rl_kill_text (rl_point, rl_mark);
  808.   return (0);
  809. }
  810.  
  811. rl_vi_change_to (count, key)
  812.      int count, key;
  813. {
  814.   int c, start_pos;
  815.  
  816.   if (uppercase_p (key))
  817.     rl_stuff_char ('$');
  818.   else if (vi_redoing)
  819.     rl_stuff_char (_rl_vi_last_motion);
  820.  
  821.   start_pos = rl_point;
  822.  
  823.   if (rl_vi_domove (key, &c))
  824.     {
  825.       ding ();
  826.       return -1;
  827.     }
  828.  
  829.   /* These are the motion commands that do not require adjusting the
  830.      mark.  c[wW] are handled by special-case code in rl_vi_domove(),
  831.      and already leave the mark at the correct location. */
  832.   if ((strchr (" l|hwW^0bB", c) == 0) && (rl_mark < rl_end))
  833.     rl_mark++;
  834.  
  835.   /* The cursor never moves with c[wW]. */
  836.   if ((to_upper (c) == 'W') && rl_point < start_pos)
  837.     rl_point = start_pos;
  838.  
  839.   rl_kill_text (rl_point, rl_mark);
  840.  
  841.   rl_begin_undo_group ();
  842.   _rl_vi_doing_insert = 1;
  843.   _rl_vi_set_last (key, count, rl_arg_sign);
  844.   rl_vi_insertion_mode (1, key);
  845.  
  846.   return (0);
  847. }
  848.  
  849. rl_vi_yank_to (count, key)
  850.      int count, key;
  851. {
  852.   int c, save = rl_point;
  853.  
  854.   if (uppercase_p (key))
  855.     rl_stuff_char ('$');
  856.  
  857.   if (rl_vi_domove (key, &c))
  858.     {
  859.       ding ();
  860.       return -1;
  861.     }
  862.  
  863.   /* These are the motion commands that do not require adjusting the
  864.      mark. */
  865.   if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
  866.     rl_mark++;
  867.  
  868.   rl_begin_undo_group ();
  869.   rl_kill_text (rl_point, rl_mark);
  870.   rl_end_undo_group ();
  871.   rl_do_undo ();
  872.   rl_point = save;
  873.  
  874.   return (0);
  875. }
  876.  
  877. rl_vi_delete (count, key)
  878.      int count, key;
  879. {
  880.   int end;
  881.  
  882.   if (rl_end == 0)
  883.     {
  884.       ding ();
  885.       return -1;
  886.     }
  887.  
  888.   end = rl_point + count;
  889.  
  890.   if (end >= rl_end)
  891.     end = rl_end;
  892.  
  893.   rl_kill_text (rl_point, end);
  894.   
  895.   if (rl_point > 0 && rl_point == rl_end)
  896.     rl_backward (1);
  897.   return (0);
  898. }
  899.  
  900. /* Turn the current line into a comment in shell history.
  901.    A K*rn shell style function. */
  902. rl_vi_comment (count, key)
  903.      int count, key;
  904. {
  905.   rl_beg_of_line ();
  906.  
  907.   if (rl_vi_comment_begin != (char *)NULL)
  908.     rl_insert_text (rl_vi_comment_begin);
  909.   else
  910.     rl_insert_text (VI_COMMENT_BEGIN_DEFAULT);    /* Default. */
  911.  
  912.   rl_redisplay ();
  913.   rl_newline (1, '\n');
  914.   return (0);
  915. }
  916.  
  917. rl_vi_first_print (count, key)
  918.      int count, key;
  919. {
  920.   return (rl_back_to_indent ());
  921. }
  922.  
  923. rl_back_to_indent (ignore1, ignore2)
  924.      int ignore1, ignore2;
  925. {
  926.   rl_beg_of_line ();
  927.   while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  928.     rl_point++;
  929.   return (0);
  930. }
  931.  
  932. /* NOTE: it is necessary that opposite directions are inverses */
  933. #define    FTO     1        /* forward to */
  934. #define BTO    -1        /* backward to */
  935. #define FFIND     2        /* forward find */
  936. #define BFIND    -2        /* backward find */
  937.  
  938. rl_vi_char_search (count, key)
  939.      int count, key;
  940. {
  941.   static char target;
  942.   static int orig_dir, dir;
  943.   int pos;
  944.  
  945.   if (key == ';' || key == ',')
  946.     dir = (key == ';' ? orig_dir : -orig_dir);
  947.   else
  948.     {
  949.       if (vi_redoing)
  950.     target = _rl_vi_last_search_char;
  951.       else
  952.     _rl_vi_last_search_char = target = rl_getc (rl_instream);
  953.  
  954.       switch (key)
  955.         {
  956.         case 't':
  957.           orig_dir = dir = FTO;
  958.           break;
  959.  
  960.         case 'T':
  961.           orig_dir = dir = BTO;
  962.           break;
  963.  
  964.         case 'f':
  965.           orig_dir = dir = FFIND;
  966.           break;
  967.  
  968.         case 'F':
  969.           orig_dir = dir = BFIND;
  970.           break;
  971.         }
  972.     }
  973.  
  974.   pos = rl_point;
  975.  
  976.   while (count--)
  977.     {
  978.       if (dir < 0)
  979.     {
  980.       if (pos == 0)
  981.         {
  982.           ding ();
  983.           return -1;
  984.         }
  985.  
  986.       pos--;
  987.       do
  988.         {
  989.           if (rl_line_buffer[pos] == target)
  990.         {
  991.           if (dir == BTO)
  992.             rl_point = pos + 1;
  993.           else
  994.             rl_point = pos;
  995.           break;
  996.         }
  997.         }
  998.       while (pos--);
  999.  
  1000.       if (pos < 0)
  1001.         {
  1002.           ding ();
  1003.           return -1;
  1004.         }
  1005.     }
  1006.       else
  1007.     {            /* dir > 0 */
  1008.       if (pos >= rl_end)
  1009.         {
  1010.           ding ();
  1011.           return -1;
  1012.         }
  1013.  
  1014.       pos++;
  1015.       do
  1016.         {
  1017.           if (rl_line_buffer[pos] == target)
  1018.         {
  1019.           if (dir == FTO)
  1020.             rl_point = pos - 1;
  1021.           else
  1022.             rl_point = pos;
  1023.           break;
  1024.         }
  1025.         }
  1026.       while (++pos < rl_end);
  1027.  
  1028.       if (pos >= (rl_end - 1))
  1029.         {
  1030.           ding ();
  1031.           return -1;
  1032.         }
  1033.     }
  1034.     }
  1035.   return (0);
  1036. }
  1037.  
  1038. /* Match brackets */
  1039. rl_vi_match (ignore, key)
  1040.      int ignore, key;
  1041. {
  1042.   int count = 1, brack, pos;
  1043.  
  1044.   pos = rl_point;
  1045.   if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
  1046.     {
  1047.       while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
  1048.          rl_point < rl_end - 1)
  1049.     rl_forward (1);
  1050.  
  1051.       if (brack <= 0)
  1052.     {
  1053.       rl_point = pos;
  1054.       ding ();
  1055.       return -1;
  1056.     }
  1057.     }
  1058.  
  1059.   pos = rl_point;
  1060.  
  1061.   if (brack < 0)
  1062.     {
  1063.       while (count)
  1064.     {
  1065.       if (--pos >= 0)
  1066.         {
  1067.           int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1068.           if (b == -brack)
  1069.         count--;
  1070.           else if (b == brack)
  1071.         count++;
  1072.         }
  1073.       else
  1074.         {
  1075.           ding ();
  1076.           return -1;
  1077.         }
  1078.     }
  1079.     }
  1080.   else
  1081.     {            /* brack > 0 */
  1082.       while (count)
  1083.     {
  1084.       if (++pos < rl_end)
  1085.         {
  1086.           int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1087.           if (b == -brack)
  1088.         count--;
  1089.           else if (b == brack)
  1090.         count++;
  1091.         }
  1092.       else
  1093.         {
  1094.           ding ();
  1095.           return -1;
  1096.         }
  1097.     }
  1098.     }
  1099.   rl_point = pos;
  1100.   return (0);
  1101. }
  1102.  
  1103. int
  1104. rl_vi_bracktype (c)
  1105.      int c;
  1106. {
  1107.   switch (c)
  1108.     {
  1109.     case '(': return  1;
  1110.     case ')': return -1;
  1111.     case '[': return  2;
  1112.     case ']': return -2;
  1113.     case '{': return  3;
  1114.     case '}': return -3;
  1115.     default:  return  0;
  1116.     }
  1117. }
  1118.  
  1119. rl_vi_change_char (count, key)
  1120.      int count, key;
  1121. {
  1122.   int c;
  1123.  
  1124.   if (vi_redoing)
  1125.     c = _rl_vi_last_replacement;
  1126.   else
  1127.     _rl_vi_last_replacement = c = rl_getc (rl_instream);
  1128.  
  1129.   if (c == '\033' || c == CTRL ('C'))
  1130.     return -1;
  1131.  
  1132.   while (count-- && rl_point < rl_end)
  1133.     {
  1134.       rl_begin_undo_group ();
  1135.  
  1136.       rl_delete (1, c);
  1137.       rl_insert (1, c);
  1138.       if (count == 0)
  1139.     rl_backward (1);
  1140.  
  1141.       rl_end_undo_group ();
  1142.     }
  1143.   return (0);
  1144. }
  1145.  
  1146. rl_vi_subst (count, key)
  1147.      int count, key;
  1148. {
  1149.   rl_begin_undo_group ();
  1150.  
  1151.   if (uppercase_p (key))
  1152.     {
  1153.       rl_beg_of_line ();
  1154.       rl_kill_line (1);
  1155.     }
  1156.   else
  1157.     rl_delete_text (rl_point, rl_point+count);
  1158.  
  1159.   rl_end_undo_group ();
  1160.  
  1161.   _rl_vi_set_last (key, count, rl_arg_sign);
  1162.  
  1163.   rl_begin_undo_group ();
  1164.   _rl_vi_doing_insert = 1;
  1165.   rl_vi_insertion_mode (1, key);
  1166.  
  1167.   return (0);
  1168. }
  1169.  
  1170. rl_vi_overstrike (count, key)
  1171.      int count, key;
  1172. {
  1173.   int i;
  1174.  
  1175.   if (_rl_vi_doing_insert == 0)
  1176.     {
  1177.       _rl_vi_doing_insert = 1;
  1178.       rl_begin_undo_group ();
  1179.     }
  1180.  
  1181.   for (i = 0; i < count; i++)
  1182.     {
  1183.       vi_replace_count++;
  1184.       rl_begin_undo_group ();
  1185.  
  1186.       if (rl_point < rl_end)
  1187.     {
  1188.       rl_delete (1, key);
  1189.       rl_insert (1, key);
  1190.     }
  1191.       else
  1192.     rl_insert (1, key);
  1193.  
  1194.       rl_end_undo_group ();
  1195.     }
  1196.   return (0);
  1197. }
  1198.  
  1199. rl_vi_overstrike_delete (count)
  1200.      int count;
  1201. {
  1202.   int i, s;
  1203.  
  1204.   for (i = 0; i < count; i++)
  1205.     {
  1206.       if (vi_replace_count == 0)
  1207.     {
  1208.       ding ();
  1209.       break;
  1210.     }
  1211.       s = rl_point;
  1212.  
  1213.       if (rl_do_undo ())
  1214.     vi_replace_count--;
  1215.  
  1216.       if (rl_point == s)
  1217.     rl_backward (1);
  1218.     }
  1219.  
  1220.   if (vi_replace_count == 0 && _rl_vi_doing_insert)
  1221.     {
  1222.       rl_end_undo_group ();
  1223.       rl_do_undo ();
  1224.       _rl_vi_doing_insert = 0;
  1225.     }
  1226.   return (0);
  1227. }
  1228.  
  1229. rl_vi_replace (count, key)
  1230.      int count, key;
  1231. {
  1232.   int i;
  1233.  
  1234.   vi_replace_count = 0;
  1235.  
  1236.   if (!vi_replace_map)
  1237.     {
  1238.       vi_replace_map = rl_make_bare_keymap ();
  1239.  
  1240.       for (i = ' '; i < KEYMAP_SIZE; i++)
  1241.     vi_replace_map[i].function = rl_vi_overstrike;
  1242.  
  1243.       vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
  1244.       vi_replace_map[ESC].function = rl_vi_movement_mode;
  1245.       vi_replace_map[RETURN].function = rl_newline;
  1246.       vi_replace_map[NEWLINE].function = rl_newline;
  1247.  
  1248.       /* If the normal vi insertion keymap has ^H bound to erase, do the
  1249.          same here.  Probably should remove the assignment to RUBOUT up
  1250.          there, but I don't think it will make a difference in real life. */
  1251.       if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC &&
  1252.       vi_insertion_keymap[CTRL ('H')].function == rl_rubout)
  1253.     vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete;
  1254.  
  1255.     }
  1256.   _rl_keymap = vi_replace_map;
  1257.   return (0);
  1258. }
  1259.  
  1260. #if 0
  1261. /* Try to complete the word we are standing on or the word that ends with
  1262.    the previous character.  A space matches everything.  Word delimiters are
  1263.    space and ;. */
  1264. rl_vi_possible_completions()
  1265. {
  1266.   int save_pos = rl_point;
  1267.  
  1268.   if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';')
  1269.     {
  1270.       while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' &&
  1271.          rl_line_buffer[rl_point] != ';')
  1272.     rl_point++;
  1273.     }
  1274.   else if (rl_line_buffer[rl_point - 1] == ';')
  1275.     {
  1276.       ding ();
  1277.       return (0);
  1278.     }
  1279.  
  1280.   rl_possible_completions ();
  1281.   rl_point = save_pos;
  1282.  
  1283.   return (0);
  1284. }
  1285. #endif
  1286.  
  1287. #if defined (STATIC_MALLOC)
  1288.  
  1289. /* **************************************************************** */
  1290. /*                                    */
  1291. /*            xmalloc and xrealloc ()                     */
  1292. /*                                    */
  1293. /* **************************************************************** */
  1294.  
  1295. static void memory_error_and_abort ();
  1296.  
  1297. static char *
  1298. xmalloc (bytes)
  1299.      int bytes;
  1300. {
  1301.   char *temp = (char *)malloc (bytes);
  1302.  
  1303.   if (!temp)
  1304.     memory_error_and_abort ();
  1305.   return (temp);
  1306. }
  1307.  
  1308. static char *
  1309. xrealloc (pointer, bytes)
  1310.      char *pointer;
  1311.      int bytes;
  1312. {
  1313.   char *temp;
  1314.  
  1315.   if (!pointer)
  1316.     temp = (char *)xmalloc (bytes);
  1317.   else
  1318.     temp = (char *)realloc (pointer, bytes);
  1319.  
  1320.   if (!temp)
  1321.     memory_error_and_abort ();
  1322.  
  1323.   return (temp);
  1324. }
  1325.  
  1326. static void
  1327. memory_error_and_abort ()
  1328. {
  1329.   fprintf (stderr, "readline: Out of virtual memory!\n");
  1330.   abort ();
  1331. }
  1332. #endif /* STATIC_MALLOC */
  1333.  
  1334. #endif /* VI_MODE */
  1335.